home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 737 / ansi / ansi.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  37KB  |  1,232 lines

  1. /***************************************************************************
  2.  
  3.    Program:    ansi
  4.    File:       ansi.c
  5.    
  6.    Version:    V1.6
  7.    Date:       16.12.91
  8.    Function:   Convert C source to and from ANSI form.
  9.    
  10.    Copyright:  SciTech Software 1991
  11.    Author:     Andrew C. R. Martin
  12.    Address:    SciTech Software
  13.                23, Stag Leys,
  14.                Ashtead,
  15.                Surrey,
  16.                KT21 2TD.
  17.    Phone:      +44 (0372) 275775
  18.    EMail:      UUCP: cbmuk!cbmuka!scitec!amartin
  19.                JANET: andrew@uk.ac.ox.biop
  20.                
  21. ****************************************************************************
  22.  
  23.    This program is not in the public domain, but it may be freely copied
  24.    and distributed for no charge providing this header is included.
  25.    The code may be modified as required, but any modifications must be
  26.    documented so that the person responsible can be identified. If someone
  27.    else breaks this code, I don't want to be blamed for code that does not
  28.    work! The code may not be sold commercially without prior permission from
  29.    the author, although it may be given away free with commercial products,
  30.    providing it is made clear that this program is free and that the source
  31.    code is provided with the program.
  32.  
  33. ****************************************************************************
  34.  
  35.    Description:
  36.    ============
  37.  
  38.    This program alters function definitions to convert non-ANSI C code to 
  39.    ANSI form. The -k and -p flags allow conversion from ANSI to K&R and
  40.    generation of prototypes respectively.
  41.    
  42.    There are two *minor* problems:
  43.    1. In generation of prototypes. If a function has been defined with no 
  44.    explicit type it defaults to being int. Strictly the prototype should 
  45.    explicitly state this is int, but doesn't.
  46.    2. If a conversion actually occurs (either to or from ANSI) any comments
  47.    which were in the definition will be lost.
  48.    
  49.    The only restriction (that I can think of!) on the code being processed
  50.    is that a function definition must be the first thing on a line.
  51.    i.e. if a comment is placed on the same line as the definition but before
  52.    it, the program will think the whole line is a comment.
  53.    
  54. ****************************************************************************
  55.  
  56.    Usage:
  57.    ======
  58.  
  59.    ansi [-k -p] <in.c> <out.c>
  60.          -k generates K&R form code from ANSI
  61.          -p generates a set of prototypes
  62.  
  63. ****************************************************************************
  64.  
  65.    Revision History:
  66.    =================
  67.    
  68.    V1.0  17.12.91
  69.    Added support for prototype and K&R code generation. Also reorganised 
  70.    some code.
  71.    
  72.    V1.1  21.01.92
  73.    A little tidying for VAX and slightly more useful error messages when
  74.    making ANSI and failing to find parameter definition---this spots bugs!
  75.    
  76.    V1.2  14.02.92
  77.    Fixed a bug whereby variable names which were subsets of the associated
  78.    type were not being found correctly.
  79.    e.g. func(o,w) struct obs *o; struct wor *w; { }
  80.    did not inherit *'s correctly in the ANSI version.
  81.    Introduced FindVarName() for this purpose.
  82.    Also added version string.
  83.  
  84.    V1.3  19.02.92
  85.    Fixed reported bug in Ansify() where KR definitions containing
  86.    comments between variables with a single type declaration were handled 
  87.    wrongly.
  88.    
  89.    V1.4  18.03.92
  90.    Fixed bug reported by Bob Bruccoleri in process_file(). If a comment
  91.    on the line of an external variable definition contained a (, the code
  92.    would think this was a prototype definition and get very confused.
  93.    Now kills the comments before testing for the presence of a (.
  94.    Improved function definition comments.
  95.    
  96.    V1.5  26.03.92
  97.    Fixed same bug as in V1.2, but for ANSI-->K&R; variable names, if a
  98.    subset of a type name were getting picked up incorrectly.
  99.    
  100.    V1.6  01.04.92
  101.    Small change to comments to work with my autodoc program.
  102.    
  103. ***************************************************************************/
  104. /* System includes
  105. */
  106. #include <stdio.h>
  107. #include <string.h>
  108.  
  109. #ifdef AMIGA
  110. #include <exec/types.h>
  111.  
  112. #else /* Not an Amiga */
  113. typedef short BOOL;
  114. #ifndef TRUE
  115. #define TRUE  1
  116. #define FALSE 0
  117. #endif
  118. #endif
  119.  
  120. /**************************************************************************/
  121. #define MAXBUFF      200   /* Max chars in a line                         */
  122. #define MAXLINES     50    /* Max lines in a function definition          */
  123. #define DIC          34    /* Double inverted commas                      */
  124. #define SIC          39    /* Single inverted commas                      */
  125. #define LF           10    /* Line feed                                   */
  126. #define CR           13    /* Carriage return                             */
  127. #define MakeANSI     1     /* K&R-->ANSI                                  */
  128. #define MakeKR       2     /* ANSI-->K&R                                  */
  129. #define MakeProtos   3     /* Make prototypes                             */
  130.  
  131. #define toggle(x) (x) = abs((x)-1)
  132.  
  133. /**************************************************************************/
  134. /* Prototypes
  135. */
  136. int   main(int argc,
  137.            char **argv);
  138. int   GetVarName(char *buffer,
  139.                  char *strparam);
  140. void  process_file(FILE *fp_in,
  141.                    FILE *fp_out,
  142.                    int mode);
  143. int   isInteresting(char *buffer);
  144. void  Ansify(FILE *fp,
  145.              char funcdef[MAXLINES][MAXBUFF], 
  146.              int ndef,
  147.              int mode);
  148. int   WriteANSI(FILE *fp,
  149.               char *varname,
  150.               char *definitions);
  151. char  *FindString(char *buffer,
  152.                   char *string);
  153. char  *FindVarName(char *buffer,
  154.                    char *string);
  155. int   isFunc(char funcdef[MAXLINES][MAXBUFF], 
  156.             int ndef);
  157. void  terminate(char *string);
  158. void  DeAnsify(FILE *fp_out, 
  159.                char funcdef[MAXLINES][MAXBUFF], 
  160.                int  ndef);
  161. void  WriteKR(FILE *fp,
  162.               char *varname,
  163.               char *definitions);
  164. void  KillComments(char *buffer);
  165. /**************************************************************************/
  166. /* Version string
  167. */
  168. #ifdef AMIGA
  169. UBYTE *vers="\0$VER: ansi 1.5";
  170. #endif
  171.  
  172. /**************************************************************************/
  173. main(int  argc,
  174.      char **argv)
  175. {
  176.    int   mode        = MakeANSI;
  177.    BOOL  noisy       = TRUE;
  178.    FILE  *fp_in      = NULL,
  179.          *fp_out     = NULL;
  180.    
  181.    if(argc < 3)
  182.    {
  183.       printf("\nUsage: ansi [-k -p -q] <in.c> <out.c>\n");
  184.       printf("       Converts a K&R style C file to ANSI or vice versa\n");
  185.       printf("       -k generates K&R form code from ANSI\n");
  186.       printf("       -p generates a set of prototypes\n");
  187.       printf("       -q quiet mode\n\n");
  188.       
  189.       exit(0);
  190.    }
  191.    
  192.    /* Parse the command line */
  193.    argv++;
  194.    while(--argc > 2)
  195.    {
  196.       if(argv[0][0] == '-')
  197.       {
  198.          switch(argv[0][1])
  199.          {
  200.          case 'k':
  201.          case 'K':
  202.             mode = MakeKR;
  203.             break;
  204.          case 'p':
  205.          case 'P':
  206.             mode = MakeProtos;
  207.             break;
  208.          case 'q':
  209.          case 'Q':
  210.             noisy = FALSE;
  211.             break;
  212.          default:
  213.             printf("Unknown switch %s\n",argv[0]);
  214.             exit(0);
  215.          }
  216.       }
  217.       else
  218.       {
  219.          printf("Invalid switch %s\n",argv[0]);
  220.          exit(0);
  221.       }
  222.       argv++;
  223.    }
  224.    
  225.    /* Open files */
  226.    if((fp_in = fopen(argv[0],"r")) == NULL)
  227.    {
  228.       printf("Unable to open input file %s\n",argv[0]);
  229.       exit(1);
  230.    }
  231.    if((fp_out = fopen(argv[1],"w")) == NULL)
  232.    {
  233.       printf("Unable to open output file %s\n",argv[1]);
  234.       exit(1);
  235.    }
  236.  
  237.    /* Give a message */
  238.    if(noisy)
  239.    {
  240.       printf("SciTech Software ansi C converter V1.5\n");
  241.       printf("Copyright (C) 1991 SciTech Software. All Rights Reserved.\n");
  242.       printf("This program is freely distributable providing no profit is made in so doing.\n\n");
  243.       switch(mode)
  244.       {
  245.       case MakeANSI:
  246.          printf("Converting file %s to ANSI\n",argv[0]);
  247.          break;
  248.       case MakeKR:
  249.          printf("Converting file %s to Kernighan and Ritchie\n",argv[0]);
  250.          break;
  251.       case MakeProtos:
  252.          printf("Generating prototypes for file %s\n",argv[0]);
  253.          break;
  254.       default:
  255.          break;
  256.       }
  257.    }
  258.  
  259.    /* Now process the files as required by the flags */
  260.    process_file(fp_in, fp_out, mode);
  261.    
  262.    exit(0);    /* V1.1, for VAX clean-ness */
  263.    return(0);
  264. }
  265.  
  266. /*>**********************************************************************
  267. GetVarName(buffer, strparam)
  268. ----------------------------
  269. Input:   char     *buffer        A character string
  270. Output:  char     *strparam      Returned character string
  271. Returns: int                     Number of characters pulled out
  272.                                  of the buffer string
  273.  
  274. This routine returns the first , or ) delimited group of characters
  275. from character string `buffer'
  276. *************************************************************************/
  277. int GetVarName(char *buffer,
  278.                char *strparam)
  279. {
  280.    int   i,
  281.          j  = 0;
  282.  
  283.    for(i=0;buffer[i];i++)
  284.    {
  285.       /* Break out if we've got a , or ) */
  286.       if(buffer[i]==',' || buffer[i]==')') break;
  287.  
  288.       /* Otherwise copy the character */
  289.       strparam[j++] = buffer[i];
  290.    }
  291.    strparam[j]='\0';
  292.    
  293.    /* Strip any trailing spaces */
  294.    for(j=strlen(strparam) - 1 ;
  295.        j >= 0 && (strparam[j] == ' ' || strparam[j] == '\t');
  296.        j--)
  297.       strparam[j] = '\0';
  298.  
  299.    return(i);
  300. }
  301.  
  302.  
  303. /*>*************************************************************************
  304. process_file(fp_in, fp_out, mode)
  305. ---------------------------------
  306. Input:   FILE     *fp_in         File to be processed
  307.          FILE     *fp_out        Output file being created
  308.          int      mode           Processing mode.
  309.                                  MakeANSI:   Create ANSI
  310.                                  MakeKR:     Create K&R
  311.                                  MakeProtos: Create prototypes
  312. Returns: void
  313.  
  314. Does the work of processing the file. Calls routines to see if this line is
  315. interesting. If so assembles the function or prototype definition. Calls
  316. check to see if its really a function definition and, if so, routines to
  317. process and convert.
  318. ***************************************************************************/
  319. void process_file(FILE *fp_in,
  320.                   FILE *fp_out,
  321.                   int mode)
  322. {
  323.    /* These are static so they're not placed on the stack. This lets
  324.       us run with the default stack size on the Amiga
  325.    */
  326.    static char buffer[MAXBUFF],
  327.                buffer2[MAXBUFF],             /* V1.4 */
  328.                funcdef[MAXLINES][MAXBUFF];
  329.    int  i,
  330.         ndef;
  331.    
  332.    while(fgets(buffer,MAXBUFF,fp_in))
  333.    {
  334.       terminate(buffer);
  335.  
  336.       /* See if this line is possibly a function definition */
  337.       if(isInteresting(buffer))
  338.       {
  339.          /* It's one of:
  340.             (a)   A function definition
  341.             (b)   A prototype
  342.             (c)   An external
  343.             
  344.             To be a function, it must contain a (, though this could be
  345.             a prototype.
  346.          */
  347.  
  348.          /* V1.4+: Previously would think the line was a function or
  349.             prototype if there was a ( in a comment on the same line
  350.          */
  351.          strcpy(buffer2,buffer);
  352.          KillComments(buffer2);
  353.          /* V1.4- */
  354.          
  355.          if(strchr(buffer2,'(') != NULL)  /* V1.4 Test buffer2 */
  356.          {
  357.             /* It's a function or a prototype. Copy it into funcdef
  358.                assembling additional strings up to the first ; or {
  359.             */
  360.             strcpy(funcdef[0], buffer);
  361.             ndef=0;
  362.             while(strchr(funcdef[ndef],';') == NULL  &&
  363.                   strchr(funcdef[ndef],'{') == NULL)
  364.             {
  365.                if(!fgets(funcdef[++ndef],MAXBUFF,fp_in)) break;
  366.                terminate(funcdef[ndef]);
  367.                if(ndef >= MAXLINES)
  368.                {
  369.                   printf("Too many lines in function definition:\n");
  370.                   for(i=0; i<MAXLINES; i++)
  371.                      printf("%s\n",funcdef[i]);
  372.                   exit(1);
  373.                }
  374.                /* Pass the string to isInteresting() to update internal
  375.                   count of comments, brackets, etc. We don't care about
  376.                   the return value.
  377.                */
  378.                isInteresting(funcdef[ndef]);
  379.             }
  380.  
  381.             if(isFunc(funcdef,ndef))
  382.             {
  383.                /* It's actually a function.
  384.                   If it was terminated by a ; we must assemble up to
  385.                   a {
  386.                */
  387.                if(strchr(funcdef[ndef],';') != NULL  &&
  388.                   strchr(funcdef[ndef],'{') == NULL)
  389.                {
  390.                   while(strchr(funcdef[ndef],'{') == NULL)
  391.                   {
  392.                      if(!fgets(funcdef[++ndef],MAXBUFF,fp_in)) break;
  393.                      if(ndef >= MAXLINES)
  394.                      {
  395.                         printf("Too many lines in function definition:\n");
  396.                         for(i=0; i<MAXLINES; i++)
  397.                            printf("%s\n",funcdef[i]);
  398.                         exit(1);
  399.                      }
  400.                      terminate(funcdef[ndef]);
  401.                      /* Pass the string to isInteresting() to update 
  402.                         internal count of comments, brackets, etc. We 
  403.                         don't care about the return value.
  404.                      */
  405.                      isInteresting(funcdef[ndef]);
  406.                   }
  407.                }
  408.                
  409.                /* Now actually ANSIfy, deANSIfy, or generate prototypes.
  410.                   Output to fp_out
  411.                */
  412.                switch(mode)
  413.                {
  414.                case MakeKR:
  415.                   DeAnsify(fp_out, funcdef, ndef);
  416.                   break;
  417.                case MakeANSI:
  418.                case MakeProtos:
  419.                   Ansify(fp_out, funcdef, ndef, mode);
  420.                   break;
  421.                default:
  422.                   printf("Internal confusion!!!\n");
  423.                   break;
  424.                }
  425.             }
  426.             else
  427.             {
  428.                /* It's a prototype, so copy each line out */
  429.                if(mode != MakeProtos)
  430.                {
  431.                   for(i=0; i<=ndef; i++)
  432.                      fprintf(fp_out,"%s\n",funcdef[i]);
  433.                }
  434.             }
  435.          }
  436.          else
  437.          {
  438.             /* It's an extern, so just copy it */
  439.             if(mode != MakeProtos) fprintf(fp_out,"%s\n",buffer);
  440.          }
  441.       }
  442.       else
  443.       {
  444.          /* We're in a #, comment, string, function or blank line.
  445.             Simply copy the line to the output file.
  446.          */
  447.          if(mode != MakeProtos) fprintf(fp_out,"%s\n",buffer);
  448.       }
  449.    }
  450. }
  451.  
  452. /*>*************************************************************************
  453. isInteresting(buffer)
  454. ---------------------
  455. Input:   char     *buffer        Line from file
  456. Returns: int                     1: Line is interesting-may be a function
  457.                                  0: Line not interesting
  458.  
  459. Tries to determine whether a line is possibly a function definition.
  460. Does this by checking, on entry, that we're not a blank line, not in a 
  461. comment, between double or single inverted commas and not already in a 
  462. function definition.
  463. ***************************************************************************/
  464. int isInteresting(char *buffer)
  465. {
  466.    static int  comment_count  = 0,
  467.                bra_count      = 0,
  468.                inSIC          = 0,
  469.                inDIC          = 0;
  470.    
  471.    int i,
  472.        retval  = 0,
  473.        isBlank = TRUE;
  474.  
  475.    /* Not interested if it's a #define, etc. */
  476.    if(buffer[0] == '#') return(0);
  477.  
  478.    /* If all of these are unset when we enter, we're interested */
  479.    if(!bra_count && !inDIC && !inSIC && !comment_count) retval = 1;
  480.  
  481.    /* If the first thing in this string was a comment we're no longer
  482.       interested.
  483.    */
  484.    for(i=0; buffer[i] && (buffer[i] == ' ' || buffer[i] == '\t'); i++);
  485.    if(buffer[i] == '/' && buffer[i+1] == '*') retval = 0;
  486.  
  487.    /* Step along the line */
  488.    for(i=0; i<strlen(buffer); i++)
  489.    {
  490.       /* We're not interested in anything else if this is a
  491.          C++ style comment
  492.       */
  493.       if(buffer[i] == '/' && buffer[i+1] == '/') return(0);
  494.  
  495.       if(buffer[i] != ' ' && buffer[i] != '\t') isBlank = FALSE;
  496.       
  497.       /* See if we're moving into a string */
  498.       if((buffer[i] == DIC) && (comment_count==0) && !inSIC) toggle(inDIC);
  499.       if((buffer[i] == SIC) && (comment_count==0) && !inDIC) 
  500.       {
  501.          toggle(inSIC);
  502.       }
  503.       
  504.       /* If we're not in a string */
  505.       if(!inDIC && !inSIC)
  506.       {
  507.          /* See if we're moving into a comment */
  508.          if((buffer[i] == '/') && (buffer[i+1] == '*')) comment_count++;
  509.          /* See if we're moving out of a comment */
  510.          if((buffer[i] == '*') && (buffer[i+1] == '/')) comment_count--;
  511.          
  512.          /* If we're not in a comment we must be in code.
  513.             Update the curly bracket count
  514.          */
  515.          if(!comment_count)
  516.          {
  517.             if(buffer[i] == '{') bra_count++;
  518.             if(buffer[i] == '}') bra_count--;
  519.          }
  520.       }
  521.    }
  522.    
  523.    /* If it's a blank line, we're not interested */
  524.    if(isBlank) retval = 0;
  525.  
  526.    return(retval);
  527. }
  528.          
  529.  
  530. /*>*************************************************************************
  531. Ansify(fp, funcdef, ndef, mode)
  532. -------------------------------
  533. Input:   FILE     *fp            File to create
  534.          char     funcdef[][]    Function definition lines
  535.          int      ndef           Number of definition lines
  536.          int      mode           Processing mode-generate ANSI or prototypes
  537.                                  MakeANSI:   Create ANSI
  538.                                  MakeProtos: Create prototypes
  539.  
  540. If it's already ANSI, just writes it; otherwise assembles function into
  541. a single buffer line, writes the function name and calls WriteANSI() to
  542. write the definition of each variable.
  543. ***************************************************************************/
  544. void Ansify(FILE *fp,
  545.             char funcdef[MAXLINES][MAXBUFF],
  546.             int  ndef,
  547.             int  mode)
  548. {
  549.    int   i,
  550.          j,
  551.          width,
  552.          isANSI   = TRUE,
  553.          bufflen  = 0,
  554.          first    = TRUE;
  555.    char  *buffer  = NULL,
  556.          *bufptr,
  557.          *funptr,
  558.          temp[MAXBUFF],
  559.          func[MAXBUFF],
  560.          varname[80];
  561.    
  562.    ndef++;
  563.    
  564.    /* If none of the lines contains a ;, it's already ANSI */
  565.    for(i=0; i<ndef; i++)
  566.    {
  567.       if(strchr(funcdef[i], ';') != NULL)
  568.       {
  569.          isANSI = FALSE;
  570.          break;
  571.       }
  572.    }
  573.    
  574.    if(isANSI)
  575.    {
  576.       /* It's already ANSI */
  577.       if(mode == MakeANSI)
  578.       {
  579.          /* We're making ANSI, so just output it */
  580.          for(i=0; i<ndef; i++) fprintf(fp, "%s\n", funcdef[i]);
  581.       }
  582.       else  /* mode == makeProtos */
  583.       {
  584.          /* We're making prototypes, just output, but put a ; instead
  585.             of a {
  586.          */
  587.          for(i=0; i<ndef; i++)
  588.          {
  589.             for(j=0; j<strlen(funcdef[i]); j++)
  590.             {
  591.                if(funcdef[i][j] != '{')
  592.                {
  593.                   putc(funcdef[i][j], fp);
  594.                }
  595.                else
  596.                {
  597.                   putc(';', fp);
  598.                   i = ndef;
  599.                   break;
  600.                }
  601.             }
  602.             putc('\n', fp);
  603.          }
  604.       }
  605.    }
  606.    else     /* It's not ANSI, so we convert it. */
  607.    {
  608.       /* First allocate some memory */
  609.       for(i=0; i<ndef; i++) bufflen += strlen(funcdef[i]);
  610.       bufflen += 2;
  611.       buffer = (char *)malloc(bufflen * sizeof(char));
  612.       buffer[0] = '\0';
  613.       
  614.       /* Now build all the strings into the single buffer */
  615.       for(i=0; i<ndef; i++) strcat(buffer, funcdef[i]);
  616.       
  617.       /* V1.3
  618.          Remove comments
  619.       */
  620.       KillComments(buffer);
  621.  
  622.       /* Copy the function part into func */
  623.       for(i=0; buffer[i] != ')'; i++) func[i] = buffer[i];
  624.       func[i]     = ')';
  625.       func[i+1]   = '\0';
  626.       
  627.       /* Find the first (, copy up to here and print it */
  628.       for(i=0; func[i] != '('; i++) temp[i] = func[i];
  629.       temp[i]     = '(';
  630.       temp[i+1]   = '\0';
  631.       width       = strlen(temp);
  632.       fprintf(fp,"%s",temp);
  633.       
  634.       /* Set bufptr to point to the buffer excluding the function def */
  635.       bufptr = strchr(buffer, ')') + 1;
  636.       
  637.       /* Set funptr to point to start of parameter list */
  638.       funptr = strchr(func, '(') + 1;
  639.       
  640.       /* Step through the parameter list getting a parameter at a time */
  641.       first = TRUE;
  642.       while(*funptr && *funptr != ')')
  643.       {
  644.          if(!first)
  645.          {
  646.             fprintf(fp,",\n");
  647.             for(i=0;i<width;i++) fprintf(fp," ");
  648.          }
  649.          first = FALSE;
  650.          /* Kill spaces */
  651.          for( ; funptr && (*funptr == ' ' || *funptr == '\t'); funptr++) ;
  652.          /* Get a parameter */
  653.          funptr += GetVarName(funptr, varname) + 1;
  654.          /* Write the ANSI version */
  655.          if(WriteANSI(fp, varname, bufptr))   /* V1.1 */
  656.          {
  657.             /* Returns 1, if there was a problem */
  658.             temp[strlen(temp)-1] = '\0';
  659.             printf("   %s()\n",temp);
  660.          }
  661.       }
  662.       
  663.       if(mode == MakeANSI)
  664.          fprintf(fp,")\n{\n");
  665.       else  /* mode == MakeProtos */
  666.          fprintf(fp,");\n");
  667.       
  668.       /* Free memory */
  669.       free(buffer);
  670.    }
  671. }
  672.  
  673. /*>*************************************************************************
  674. WriteANSI(fp, varname, definitions)
  675. -----------------------------------
  676. Input:   FILE     *fp            File being written
  677.          char     *varname       Variable name being processed
  678.          char     *definitions   Assembled KR definitions.
  679. Returns: int                     0: if all OK; 1: if a problem
  680.  
  681. Creates an ANSI definition from the KR definition and writes it into the
  682. parameter list.
  683. ***************************************************************************/
  684. int WriteANSI(FILE *fp,
  685.               char *varname,
  686.               char *definitions)
  687. {
  688.    char  *start,
  689.          *stop,
  690.          *ptr,
  691.          buffer[MAXBUFF];
  692.    int   i;
  693.         
  694. /*** Find the variable type ***/
  695.  
  696.    /* Set these to the position of varname in the definitions list */
  697.    start = stop = FindVarName(definitions, varname);  /* V1.2 */
  698.    
  699.    if(!start)
  700.    {
  701.       printf("Parameter `%s' was not found in definitions for function:\n",varname);
  702.       return(1);
  703.    }
  704.    
  705.    /* Step start back to the start of the list, or the preceeding ; */
  706. /***V1.3+
  707. //   while(start > definitions && *start != ';' && *start != '/') start--;
  708. //   if(*start == ';' || *start == '/') start++;
  709. */
  710.    while(start > definitions && *start != ';') start--;
  711.    if(*start == ';') start++;
  712.  
  713. /***V1.3-   */
  714.    
  715.    /* Kill any leading spaces */
  716.    while(*start && (*start == ' ' || *start == '\t')) start++;
  717.    
  718.    /* If there are any commas between start and stop, move stop
  719.       back to the first comma
  720.    */
  721.    for(ptr=start; ptr<=stop; ptr++)
  722.    {
  723.       if(*ptr == ',')
  724.       {
  725.          stop = ptr;
  726.          break;
  727.       }
  728.    }
  729.    
  730.    /* Step stop on to the first , or ; */
  731.    while(*stop && *stop != ',' && *stop != ';') stop++;
  732.  
  733.    /* Now step back over any spaces */
  734.    stop--;
  735.    while(stop > start && (*stop == ' ' || *stop == '\t')) stop--;
  736.    
  737.    /* Now step back over the first variable name */
  738.    while(stop > start && *stop != ' ' && *stop != '\t') stop--;
  739.    
  740.    /* and over the spaces preceeding it */
  741.    while(stop > start && (*stop == ' ' || *stop == '\t')) stop--;
  742.    
  743.    /* Now copy the string delimited by start and stop */
  744.    for(i=0; i<MAXBUFF && start <= stop; i++, start++)
  745.       buffer[i] = *start;
  746.  
  747.    /* Terminate and print it */
  748.    buffer[i] = '\0';
  749.    fprintf(fp,"%s ",buffer);
  750.    
  751. /*** Now print the variable name with *'s if appropriate ***/
  752.  
  753.    /* Set this to the position of varname in the definitions list */
  754.    start = FindVarName(definitions, varname);  /* V1.2 */
  755.    
  756.    /* Step start back to the first non-space character */
  757.    start--;
  758.    while(start > definitions && (*start == ' ' || *start == '\t')) start--;
  759.    
  760.    while(*(start--) == '*')
  761.       fprintf(fp,"*");
  762.  
  763.    fprintf(fp,"%s",varname);
  764.    
  765. /*** Finally see if it's a [] array ***/
  766.    /* Set these to the position of varname in the definitions list */
  767.    start = stop = FindVarName(definitions, varname);  /* V1.2 */
  768.  
  769.    /* Step stop on to the first , or ; */
  770.    while(*stop && *stop != ',' && *stop != ';') stop++;
  771.  
  772.    /* Now step back over any spaces */
  773.    stop--;
  774.    while(stop > start && (*stop == ' ' || *stop == '\t')) stop--;
  775.    
  776.    /* See if there is a [ between start and stop */
  777.    while(start<stop && *start != '[') start++;
  778.    
  779.    /* If a [ was found copy and print the string */
  780.    if(start < stop)
  781.    {
  782.       for(i=0; i<MAXBUFF && start <= stop; i++, start++)
  783.          buffer[i] = *start;
  784.  
  785.       /* Terminate and print it */
  786.       buffer[i] = '\0';
  787.       fprintf(fp,"%s",buffer);
  788.    }
  789.    
  790.    return(0);  /* V1.1, all OK */
  791. }
  792.  
  793. /*>*************************************************************************
  794. FindString(buffer, string)
  795. --------------------------
  796. Input:   char     *buffer        Buffer being searched
  797.          char     *string        String to search for
  798. Returns: *char                   Pointer to start of string in buffer
  799.  
  800. Searches for a string in another string returning a pointer to the start
  801. of the string.
  802. ***************************************************************************/
  803. char *FindString(char *buffer,
  804.                  char *string)
  805. {
  806.    char  *ptr;
  807.    int   ok = FALSE,
  808.          i;
  809.    
  810.    ptr = buffer;
  811.    
  812.    while(!ok)
  813.    {
  814.       /* Step ptr along buffer till we find first character of string */
  815.       while(*ptr && *ptr != *string) ptr++;
  816.  
  817.       /* Return NULL if we didn't find it */
  818.       if(*ptr == '\0') return((char *)NULL);
  819.       
  820.       /* Now compare the rest of the string */
  821.       ok = TRUE;
  822.       for(i=0; i<strlen(string); i++)
  823.       {
  824.          if(ptr[i] != string[i])
  825.          {
  826.             ok = FALSE;
  827.             break;
  828.          }
  829.       }
  830.       ptr++;
  831.    }
  832.    return(--ptr);
  833. }
  834. /*>*************************************************************************
  835. FindVarName(buffer, string)
  836. ---------------------------
  837. Input:   char     *buffer        Buffer being searched
  838.          char     *string        String to search for
  839. Returns: *char                   Pointer to start of string in buffer
  840.  
  841. Works like FindString(), but imposes the additional condition that the
  842. string must be preceded by a space or * and must be followed by one of
  843. space ; [ ) or ,
  844.  
  845. Added for V1.2
  846. ***************************************************************************/
  847. char *FindVarName(char *buffer,
  848.                   char *string)
  849. {
  850.    char  *ptr;
  851.    int   ok = FALSE,
  852.          i;
  853.    
  854.    ptr = buffer;
  855.    
  856.    while(!ok)
  857.    {
  858.       /* Step ptr along buffer till we find first character of string with
  859.          a space or * before it.
  860.       */
  861.       ptr--;
  862.       do
  863.       {
  864.          ptr++;
  865.          while(*ptr && *ptr != *string) ptr++;
  866.       }  while(*ptr && *(ptr-1) != ' ' && *(ptr-1) != '*' && *(ptr-1) != ',');
  867.  
  868.       /* Return NULL if we didn't find it */
  869.       if(*ptr == '\0') return((char *)NULL);
  870.       
  871.       /* Now compare the rest of the string */
  872.       ok = TRUE;
  873.       for(i=0; i<strlen(string); i++)
  874.       {
  875.          if(ptr[i] != string[i])
  876.          {
  877.             ok = FALSE;
  878.             break;
  879.          }
  880.       }
  881.       
  882.       /* Check the character after the string */
  883.       if(*(ptr+i) != ';' && *(ptr+i) != '[' && 
  884.          *(ptr+i) != ' ' && *(ptr+i) != ')' && *(ptr+i) != ',') ok = FALSE;
  885.  
  886.       ptr++;
  887.    }
  888.    return(--ptr);
  889. }
  890. /*>*************************************************************************
  891. isFunc(funcdef, ndef)
  892. ---------------------
  893. Input:   char     funcdef[][]    Array of lines forming function definition
  894.          int      ndef           Number of lines
  895. Returns: int                     1: This is a function
  896.                                  0: Not a function
  897.  
  898. Determines whether a possible function definition identified by 
  899. isInteresting() really is a function.
  900. ***************************************************************************/
  901. int isFunc(char funcdef[MAXLINES][MAXBUFF],
  902.            int  ndef)
  903. {
  904.    char  *termchar;
  905.    int   line,
  906.          retval;
  907.    
  908.    /* If it's a prototype, it will not be terminated by a { */
  909.    if(strchr(funcdef[ndef],'{') != NULL) return(1);
  910.    
  911.    /* It's now either a prototype or a K&R function defintion.
  912.       To be a prototype, the first non-space character before the
  913.       ; must be a )
  914.       
  915.       Step backwards.
  916.    */
  917.    line = ndef;
  918.    for(;;)
  919.    {
  920.       termchar = strchr(funcdef[line],';') - 1;
  921.       while(termchar >= funcdef[line] && 
  922.             (*termchar == ' ' || *termchar == '\t'))
  923.          termchar--;
  924.       
  925.       /* If we stepped back beyond the start of the line, go to the
  926.          previous line
  927.       */
  928.       if(termchar < funcdef[line])
  929.       {
  930.          line--;
  931.          if(line < 0) break;
  932.          termchar = funcdef[line] + strlen(funcdef[line]);
  933.       }
  934.       else
  935.       {
  936.          break;
  937.       }
  938.    }
  939.    
  940.    /* OK, see if the character was a ) */
  941.    if(*termchar == ')')
  942.       retval = 0;
  943.    else
  944.       retval = 1;
  945.       
  946.    return(retval);
  947. }
  948.  
  949. /*>***********************************************************************
  950. terminate(string)
  951. -----------------
  952. I/O:     char     *string        A character string
  953. Returns: void
  954.  
  955. Terminates a string at the first \n
  956. *************************************************************************/
  957. void terminate(char *string)
  958. {
  959.    int i;
  960.    
  961.    for(i=0;string[i];i++)
  962.    {
  963.       if(string[i] == '\n')
  964.       {
  965.          string[i] = '\0';
  966.          break;
  967.       }
  968.    }
  969. }
  970. /*>***********************************************************************
  971. DeAnsify(fp, funcdef, ndef)
  972. ---------------------------
  973. Input:   FILE     *fp            File being written
  974.          char     funcdef[][]    Function definition array
  975.          int      ndef           Number of definition lines
  976. Returns: void
  977.  
  978. Writes a K&R function definition from the ANSI (or K&R) form in funcdef.
  979. If it's already K&R, just writes it; otherwise assembles function into
  980. a single buffer line, writes the function name and calls WriteKR() to
  981. write the definition of each variable.
  982. *************************************************************************/
  983. void DeAnsify(FILE *fp, 
  984.               char funcdef[MAXLINES][MAXBUFF], 
  985.               int  ndef)
  986. {
  987.    int   i,
  988.          j,
  989.          nparam,
  990.          isKR     = FALSE,
  991.          bufflen  = 0,
  992.          last     = FALSE;
  993.    char  *buffer  = NULL,
  994.          *bufptr,
  995.          *funptr,
  996.          *ptr,
  997.          *start,
  998.          *stop,
  999.          temp[MAXBUFF],
  1000.          func[MAXBUFF],
  1001.          varname[80];
  1002.    
  1003.    ndef++;
  1004.    
  1005.    /* If any of the lines contains a ;, it's already KR */
  1006.    for(i=0; i<ndef; i++)
  1007.    {
  1008.       if(strchr(funcdef[i], ';') != NULL)
  1009.       {
  1010.          isKR = TRUE;
  1011.          break;
  1012.       }
  1013.    }
  1014.    
  1015.    if(isKR)
  1016.    {
  1017.       /* It's already KR, so just output it */
  1018.       for(i=0; i<ndef; i++) fprintf(fp, "%s\n", funcdef[i]);
  1019.    }
  1020.    else     /* It's not KR, so we convert it. */
  1021.    {
  1022.       /* First allocate some memory */
  1023.       for(i=0; i<ndef; i++) bufflen += strlen(funcdef[i]);
  1024.       bufflen += 2;
  1025.       buffer = (char *)malloc(bufflen * sizeof(char));
  1026.       buffer[0] = '\0';
  1027.       
  1028.       /* Now build all the strings into the single buffer ignoring comments */
  1029.       for(i=0; i<ndef; i++) strcat(buffer,funcdef[i]);
  1030.  
  1031.       /* Find the first (, copy up to here and print it */
  1032.       for(i=0; buffer[i] != '('; i++) temp[i] = buffer[i];
  1033.       temp[i]     = '(';
  1034.       temp[i+1]   = '\0';
  1035.       fprintf(fp,"%s",temp);
  1036.       
  1037.       /* Set bufptr to point to the buffer excluding the function name */
  1038.       bufptr = strchr(buffer, '(') + 1;
  1039.       
  1040.       /* Count the number of commas in the parameter list */
  1041.       nparam = 0;
  1042.       for(funptr = bufptr; *funptr && *funptr != ')'; funptr++)
  1043.          if(*funptr == ',') nparam++;
  1044.          
  1045.       if(nparam)
  1046.       {
  1047.          /* If there were *any* commas, the number of parameters is one
  1048.             more than the number of commas
  1049.          */
  1050.          nparam++;
  1051.       }
  1052.       else
  1053.       {
  1054.          /* If there weren't any commas, there are either 0 or 1 params.
  1055.             If there's only white space, or `void' between the ( and ) 
  1056.             there are 0 parameters. Otherwise, there's 1
  1057.          */
  1058.          if(FindString(bufptr,"void") || FindString(bufptr,"VOID"))
  1059.          {
  1060.             nparam = 0;
  1061.          }
  1062.          else
  1063.          {
  1064.             for(funptr = bufptr; *funptr && *funptr != ')'; funptr++)
  1065.             {
  1066.                if(*funptr != ' ' && *funptr != '\t')
  1067.                {
  1068.                   nparam = 1;
  1069.                   break;
  1070.                }
  1071.             }
  1072.          }
  1073.       }
  1074.       
  1075.       /* If there weren't any parameters we can just output a closing
  1076.          parenthesis an opening { and return.
  1077.       */
  1078.       if(nparam==0)
  1079.       {
  1080.          fprintf(fp,")\n{\n");
  1081.          free(buffer);
  1082.          return;
  1083.       }
  1084.  
  1085.       /* Step through the parameter list getting a parameter at a time.
  1086.          Assemble these into func.
  1087.          The variable names are delimited by a , a [ or the closing )
  1088.       */
  1089.       func[0] = '\0';
  1090.       funptr = bufptr;
  1091.       for(i=0; i<nparam; i++)
  1092.       {
  1093.          /* Step funptr on to the next , or ) */
  1094.          if((funptr = strchr(funptr,',')) == NULL)
  1095.          {
  1096.             funptr = strchr(bufptr,')');
  1097.             last = TRUE;
  1098.          }
  1099.          
  1100.          /* Step back over any spaces */
  1101.          stop = funptr-1;
  1102.          while(stop>bufptr && (*stop==' ' || *stop=='\t')) stop--;
  1103.          
  1104.          /* Step back to the start of the variable name */
  1105.          start = stop;
  1106.          while(start>=bufptr && *start!=' ' && *start!='\t' && *start != '*')
  1107.             start--;
  1108.          start++;
  1109.          
  1110.          /* Copy the variable name into our function buffer adding 
  1111.             a , and space or ) as appropriate.
  1112.          */
  1113.          for(j=0; start<=stop; start++, j++)
  1114.             temp[j] = *start;
  1115.          temp[j] = '\0';
  1116.  
  1117.          if((ptr = strchr(temp,'[')) != NULL)
  1118.             *ptr = '\0';
  1119.             
  1120.          if(last)
  1121.             strcat(temp,")");
  1122.          else
  1123.             strcat(temp,", ");
  1124.          
  1125.          strcat(func, temp);
  1126.          funptr++;
  1127.       }
  1128.       
  1129.       /* We can now echo the parameter list to the output file */
  1130.       fprintf(fp,"%s\n",func);
  1131.  
  1132.       /* Work through the parameter list writing the parameter 
  1133.          definition lines
  1134.       */
  1135.       funptr = func;
  1136.       while(*funptr && *funptr != ')')
  1137.       {
  1138.          /* Kill spaces */
  1139.          for( ; funptr && (*funptr == ' ' || *funptr == '\t'); funptr++) ;
  1140.          /* Get a parameter */
  1141.          funptr += GetVarName(funptr, varname) + 1;
  1142.          /* Write the K&R version */
  1143.          WriteKR(fp, varname, bufptr);
  1144.       }
  1145.       
  1146.       fprintf(fp,"{\n");
  1147.  
  1148.       /* Free memory */
  1149.       free(buffer);
  1150.    }
  1151. }
  1152.  
  1153. /*>*************************************************************************
  1154. WriteKR(fp, varname, definitions)
  1155. ---------------------------------
  1156. Input:   FILE     *fp            File being written
  1157.          char     *varname       Variable being processed
  1158.          char     *definitions   ANSI style definitions
  1159. Returns: void
  1160.  
  1161. Writes a variable definition in K&R form by extracting information from
  1162. the ANSI definition.
  1163. ***************************************************************************/
  1164. void WriteKR(FILE *fp,
  1165.              char *varname,
  1166.              char *definitions)
  1167. {
  1168.    char  *start,
  1169.          *stop,
  1170.          temp[MAXBUFF];
  1171.    int   i;
  1172.    
  1173.    /* Find the variable name in the definitions */
  1174. /*** V1.5+
  1175. // start = stop = FindString(definitions,varname);
  1176. */
  1177.    start = stop = FindVarName(definitions,varname);
  1178. /*** V1.5- */
  1179.    
  1180.    /* Step start back to the preceeding , / or (, then forward 
  1181.       over any spaces
  1182.    */
  1183.    while(start >= definitions && *start != '(' && 
  1184.          *start != ',' && *start != '/')
  1185.       start--;
  1186.    start++;
  1187.    while(start<stop && (*start==' ' || *start=='\t')) start++;
  1188.    
  1189.    /* Step stop on to the following , or ) */
  1190.    while(*stop && *stop != ')' && *stop != ',') stop++;
  1191.    stop--;
  1192.    
  1193.    /* Copy the variable definition, add a ; and output. */
  1194.    for(i=0; start<=stop; start++, i++)
  1195.       temp[i] = *start;
  1196.    temp[i]     = ';';
  1197.    temp[i+1]   = '\0';
  1198.  
  1199.    fprintf(fp,"%s\n",temp);
  1200. }
  1201.  
  1202. /*>*************************************************************************
  1203. KillComments(buffer)
  1204. --------------------
  1205. I/O:     char     *buffer        String from which to remove any comments
  1206. Returns: void
  1207.  
  1208. Takes a string and removes any section enclosed in comments.
  1209.  
  1210. Added for V1.3
  1211. ***************************************************************************/
  1212. void KillComments(char *buffer)
  1213. {
  1214.    int   in       = 0,
  1215.          out      = 0,
  1216.          comment  = 0,
  1217.          len;
  1218.    
  1219.    len = strlen(buffer);
  1220.    
  1221.    for(in=0;in<len;in++)
  1222.    {
  1223.       if(buffer[in]   == '/' && buffer[in+1] == '*') comment++;
  1224.       if(buffer[in-2] == '*' && buffer[in-1] == '/') comment--;
  1225.       
  1226.       if(!comment) buffer[out++] = buffer[in];
  1227.    }
  1228.    
  1229.    buffer[out] = '\0';
  1230. }
  1231.  
  1232.